package edu.gym4all.service.organizer;

import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import edu.gym4all.data.sqlite.android.dao.SQLiteTrainingPlanDAO;
import edu.gym4all.data.sqlite.android.init.SQLiteDBInit;
import edu.gym4all.data.sqlite.android.init.SQLiteTrainingInit;
import edu.gym4all.service.TrainingPlanOrganizer;
import edu.gym4all.service.domain.Cardio;
import edu.gym4all.service.domain.Training;
import edu.gym4all.service.domain.TrainingPlan;
import edu.gym4all.service.enums.CardioEnum;
import edu.gym4all.service.enums.TrainingTag;

public class TrainingPlanOrganizerImpl implements TrainingPlanOrganizer {

	Context context;

	SQLiteDatabase db;

	@Override
	public TrainingPlan createTrainingPlan() {
		return new TrainingPlan(TrainingTag.EMPTY);
	}

	@Override
	public TrainingPlan loadTrainingPlan(Context context) {
		this.context = context;
		SQLiteTrainingPlanDAO trainingPlanDAO = new SQLiteTrainingPlanDAO();
		return trainingPlanDAO.findTrainingPlan();
	}

	public List<Training> loadTrainings(TrainingPlan trainingPlan) {
		SQLiteTrainingInit trainingInit = new SQLiteTrainingInit(this.context,
				this.db);
		List<Training> trainings = trainingInit.findAll(trainingPlan);

		for (Training training : trainings) {
			training.addCardioExercise(new Cardio(CardioEnum.BIKE, 40));
			trainingPlan.setNextTag(this.nextTag(training.getTag()));
			trainingPlan.addTraining(training);
		}
		return trainings;
	}

	public TrainingTag nextTag(TrainingTag nextTag) {
		if (nextTag.equals(TrainingTag.EMPTY)) {
			nextTag = TrainingTag.A;
		} else if (nextTag.equals(TrainingTag.A)) {
			nextTag = TrainingTag.B;
		} else if (nextTag.equals(TrainingTag.B)) {
			nextTag = TrainingTag.C;
		} else if (nextTag.equals(TrainingTag.C)) {
			nextTag = TrainingTag.D;
		} else if (nextTag.equals(TrainingTag.D)) {
			nextTag = TrainingTag.E;
		} else if (nextTag.equals(TrainingTag.E)) {
			nextTag = TrainingTag.F;
		} else {
			nextTag = TrainingTag.FULL;
		}
		return nextTag;
	}

	public SQLiteDatabase initDataBase(Context context) {
		SQLiteDBInit sqliteDB = new SQLiteDBInit(context);
		this.db = sqliteDB.getWritableDatabase();
		return this.db;
	}
}